<?php
namespace App\Noxinfluencer\Controllers;
require_once '../../BaseController.php';
require_once '../../../vendor/autoload.php';

use App\BaseController;
use phpspider\core\requests;
use phpspider\core\selector;

class UserCategorySpiderController extends BaseController
{

    public function __construct()
    {

    }

    public function getUserOfNotCategory(){
        $users = $this->medooDb()->select('kol_user', [
            "[>]kol_user_category" => ["id" => "user_id"],
        ], [
            'kol_user.id',
            'kol_user.channel_id',
        ], [
            "kol_user_category.user_id" => null,
            "ORDER" => ["kol_user.id" => "ASC"],
            "LIMIT" => 1000
        ]);
        return $users;
    }


    public function startCreateUserCategory()
    {
        $medooDb = $this->medooDb();

        while (true) {

            $users = $this->getUserOfNotCategory();

            if(empty($users)){
                echo "kol类别已同步完成\n";
                sleep(600);
            }

            foreach ($users as $key => $value) {
                echo $value['id']."\n";

                $url = "https://cn.noxinfluencer.com/youtube/channel/{$value['channel_id']}";

                $spider_data = requests::get($url);

                $category = selector::select($spider_data, "//div[@class='info-container']//div[@class='info-header']//div[@class='tags']//a");

                if (!is_array($category) && !empty($category)) {
                    $category = [$category];
                }

                if (is_array($category) && !empty($category)) {
                    for ($i = 0; $i < count($category); $i++) {

                        $kol_category = $medooDb->get('kol_category', ['id'], ['category_name' => trim($category[$i])]);
                        if ($kol_category) {
                            $category_id = $kol_category['id'];
                        } else {
                            $medooDb->insert('kol_category', [
                                'category_name' => trim($category[$i]),
                                'create_time' => date('Y-m-d H:i:s', time()),
                                'update_time' => date('Y-m-d H:i:s', time()),
                            ]);
                            $category_id = $medooDb->id();
                        }

                        $medooDb->insert('kol_user_category', [
                            'user_id' => $value['id'],
                            'category_id' => $category_id,
                            'create_time' => date('Y-m-d H:i:s', time()),
                            'update_time' => date('Y-m-d H:i:s', time()),
                        ]);
                    }
                }
            }

        }

    }


}

(new UserCategorySpiderController())->startCreateUserCategory();





